//
//  SLRegisterVC.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/10.
//

#import "SLRegisterVC.h"
#import "SLPrivateAndConditionsVC.h"
#import "SLVerifyCodeVC.h"
@interface SLRegisterVC ()
@property (nonatomic, assign)BOOL isAgree;
@end

@implementation SLRegisterVC
- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.tf becomeFirstResponder];
    [self setUpSubViews];
}

-(void)setUpSubViews{
    self.title = kLocalString(@"Create account");
   
    _sureBtn.btnStyle = 1;
    [_sureBtn setTitle:kLocalString(@"Continue") forState:UIControlStateNormal];
    self.bgView.layer.cornerRadius = 20;
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.borderColor = kHexColor(@"#707070").CGColor;
    self.bgView.layer.borderWidth = 1;
    
    if (_step == 1) {
        _protectBgView.hidden = YES;
        _topTipLbl.text = kLocalString(@"What's your name?");
        [_tf setPlaceholderWithPlaceholderText:kLocalString(@"Enter your name") withColor:KColor_placeholdar withFont:kFont(16)];
        _tipLbl.text = kLocalString(@"Please enter your name which can help to identify you.");
        [_tf showCloseBtn];
    }else if (_step == 2){
        _protectBgView.hidden = YES;
        _topTipLbl.text = kLocalString(@"Email address");
        [_tf setPlaceholderWithPlaceholderText:kLocalString(@"Enter your Email") withColor:KColor_placeholdar withFont:kFont(16)];
        _tipLbl.text = kLocalString(@"This Email will be used to reveive alarm notification. You can change it later in the setting menu.");
        [_tf showCloseBtn];
    }else if (_step == 3){
        _topTipLbl.text = kLocalString(@"Create password");
        [_tf setPlaceholderWithPlaceholderText:kLocalString(@"Enter password") withColor:KColor_placeholdar withFont:kFont(16)];
        [_tf showEyeBtn];
        _tf.secureTextEntry = YES;
        _tipLbl.hidden = YES;
        _protectBgView.hidden = NO;
        [self addTextLabel];
        
        _icon.userInteractionEnabled = YES;
        kWeakSelf(self);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            weakself.isAgree = !weakself.isAgree;
            weakself.icon.image = weakself.isAgree ? [UIImage imageNamed:@"icon_check_sel"] : [UIImage imageNamed:@"icon_check_nor"];
        }];
        [_icon addGestureRecognizer:tap];
        
    }
    else if (_step == 99){
        self.title = kLocalString(@"Forgot Password");
        _topTipLbl.text = kLocalString(@"Email");
        [_tf setPlaceholderWithPlaceholderText:kLocalString(@"Enter your Email") withColor:KColor_placeholdar withFont:kFont(16)];
        _tipLbl.text = kLocalString(@"Please enter your Email that you used to creat the account.");
        _protectBgView.hidden = YES;
        [_tf showCloseBtn];
        [_sureBtn setTitle:kLocalString(@"Send verification code") forState:UIControlStateNormal];
    }
}

- (IBAction)sureBtnAction:(id)sender {
    if (_step == 1) {//创建账号输入名字
        _userModel = [[UserInfo alloc] init];
        _userModel.nickName = _tf.text;
        if (_userModel.nickName.length == 0) {
            kShowToast(@"Enter your name");
            return;
        }
        SLRegisterVC *vc = [[SLRegisterVC alloc] init];
        vc.userModel = _userModel;
        vc.step = 2;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (_step == 2){//创建账号输入邮箱
        _userModel.email = _tf.text;
        if (_userModel.email.length == 0) {
            kShowToast(@"Enter your email");
            return;
        }
        if (![_userModel.email isValidEmail]) {
            kShowToast(@"Please enter the correct email number");
            return;
        }
        SLRegisterVC *vc = [[SLRegisterVC alloc] init];
        vc.userModel = _userModel;
        vc.step = 3;
        [self.navigationController pushViewController:vc animated:YES];
    }else if(_step == 3){//输入密码
        _userModel.password = _tf.text;
        if (![_userModel.password checkPassword]) {
            return;
        }
        if (!_isAgree) {
            kShowToast(@"Please agree the user agreement");
            return;
        }
        [self sendCode:1];
    }else if (_step == 99){//忘记密码输入验证码
        _userModel = [[UserInfo alloc] init];
        _userModel.email = _tf.text;
        if (_userModel.email.length == 0) {
            kShowToast(@"Enter your email");
            return;
        }
        [self sendCode:2];
    }
}

-(void)sendCode:(NSInteger)style{
    kWeakSelf(self);
    [ZCTool showLoadingOnKeyWindow];
    NSString *url = [NSString stringWithFormat:@"/msl_user/sendMail/%@",_userModel.email];
    [ZKHttpManager postWithPath:url params:nil finished:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull code, NSString * _Nonnull message) {
        [ZCTool hideLoadingOnKeyWindow];
        BOOL isHaveSend = [code isEqualToString:@"500"];
        kShowToast(message);
        if (!isSuccessed && !isHaveSend) {
            return;
        }
        SLVerifyCodeVC *vc = [[SLVerifyCodeVC alloc] init];
        vc.style = style;
        vc.userModel = weakself.userModel;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

/*
 添加富文本点击事件
 */
-(void)addTextLabel{
    NSString *textStr = kLocalString(@"I agree to the Terms of Service and Privacy Statement");
    __block NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:textStr];
    text.color = KWhiteColor_text;
    text.font = kFont(14);
    
    NSRange range = [textStr rangeOfString:kLocalString(@"Terms of Service")];
    [text setTextHighlightRange:range color:KWhiteColor_text font: kFontBold(14) backgroundColor:KClearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull clickText, NSRange range, CGRect rect) {
        SLPrivateAndConditionsVC *vc = [[SLPrivateAndConditionsVC alloc] init];
        vc.curPage = 2;
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [text addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
    
    NSRange range2 = [textStr rangeOfString:kLocalString(@"Privacy Statement")];
    [text setTextHighlightRange:range2 color:KWhiteColor_text font: kFontBold(14) backgroundColor:KColor_B tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull clickText, NSRange range, CGRect rect) {
        SLPrivateAndConditionsVC *vc = [[SLPrivateAndConditionsVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [text addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range2];
    
    YYLabel *textLabel = [YYLabel new];
    text.lineSpacing = 6;
    textLabel.numberOfLines = 0;
    textLabel.attributedText = text;
    //设置换行方式
    NSMutableParagraphStyle  *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    text.paragraphStyle = paragraphStyle;
    
    //计算文本尺寸
    CGSize maxSize = CGSizeMake(KScreenWidth-113, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:text];
    textLabel.textLayout = layout;
    CGFloat introHeight = layout.textBoundingSize.height;
    textLabel.left = 0;
    textLabel.width = maxSize.width;
    textLabel.height = introHeight;
    
    [_proTextBgView addSubview:textLabel];
}

@end
